package cn.pconline.officialclub.web.wap;

import cn.pconline.autoclub.entity.DynaInfo;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.UserService;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.web.UserAuth;
import cn.pconline.autoclub.web.wap.ActionKit;
import cn.pconline.officialclub.entity.OActivity;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.entity.OClubTag;
import cn.pconline.officialclub.service.OActivityService;
import cn.pconline.officialclub.service.OClubService;
import cn.pconline.officialclub.service.ODynaService;
import cn.pconline.officialclub.service.OMemberService;
import cn.pconline.officialclub.service.OStatService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author xulin
 */
@Controller
@RequestMapping(value="/wap/brand/club")
public class OClubAction {
    @Autowired
    private OClubService oClubService;
    
    @Autowired
    private OStatService oStatService;
    
    @Autowired
    private OActivityService oActivityService;
    
    @Autowired
    private ODynaService oDynaService;
    
    @Autowired
    private UserAuth userAuth;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private OMemberService oMemberService;
    private static final int MAX_TOP_COUNT = 5;
    private static final int DEF_PAGE_NUM  = 1;
    private static final int DEF_PAGE_SIZE = 10;
    private static final int MAX_PAGE_SIZE = 100;
    private static final int MIN_PAGE_SIZE = 1;
    
    /**
     * WAP版品牌车友会首页
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/index.do",method= RequestMethod.GET)
    public String index(HttpServletRequest request,HttpServletResponse response){
        long oClubId =WebUtils.paramOClubId(request);
        OClub oClub = oClubService.findValidClub(oClubId);
        if(oClub == null){
            return ActionKit.SNED_ERROR_404;
        }
        User user = userAuth.getUser(request, response);
        long totalPv = oStatService.countPvByClubId(oClubId);
        //最新活动
        OActivity oActivity = oActivityService.getLatestActivity(oClubId);
        //置顶动态
        Pager<DynaInfo> topDyna = oDynaService.getTopDynaListByClubId(oClubId, MAX_TOP_COUNT);
        if(topDyna!=null && !topDyna.getResultList().isEmpty()){
            List<DynaInfo> list = topDyna.getResultList();
            for(DynaInfo dynaInfo : list){
                String t = dynaInfo.getTitle() == null ? "" : dynaInfo.getTitle().trim();
                if(dynaInfo.getType() == DynaInfo.TYPE_ACTIVITY){
                    OActivity a = oActivityService.findById(OActivity.class, dynaInfo.getTargetId());
                    t = a.getTitle(); 
                }else if (dynaInfo.getType() == DynaInfo.TYPE_COMMENT) {
                    if (StringUtils.isBlank(dynaInfo.getContent())) {
                        User u = userService.findUser(dynaInfo.getCreateBy());
                        t = u.getNickname() + "发表了一条说说";
                    } else {
                        t = dynaInfo.getContent();
                    }
                }
                 t = WebUtils.transformToBBSFaceThenLimit(t, 23);//置顶的地方最多显示23个表情的宽度+...
                 dynaInfo.setTitle(t);
            }
        }
        boolean isMember = false;
        if(user.getUid()>0){
            isMember = oMemberService.findCommonMember(user.getUid(), oClub.getoClubId())!=null;
        }
        request.setAttribute("isMember", isMember);
        request.setAttribute("user", user);
        request.setAttribute("activityCount", oActivityService.countActivity(oClubId));
        request.setAttribute("isLogin", user.getUid()>0);
        request.setAttribute("isUserLock", oMemberService.isUserLock(user, oClubId));
        request.setAttribute("topDyna", topDyna);
        request.setAttribute("oActivity", oActivity);
        request.setAttribute("totalPv", totalPv);
        request.setAttribute("oClub", oClub);
        return "template/official/wap/club/index";
    }
    /**
     * 官方车友会列表页
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/clubList.do",method= RequestMethod.GET)
    public String clubList(HttpServletRequest request,HttpServletResponse response){
        int pageNo = WebUtils.paramInt(request, "pageNo", 1);
        int pageSize = 10;
        Pager<OClub> pager = oClubService.pagerClubByTag(OClubTag.TYPE_BRAND,pageNo,pageSize,OClub.STATUS_NORMAL);
        request.setAttribute("pager", pager);
        return "template/official/wap/club/clubList";
    }
    /**
     * 查询车友会列表接口
     * @param request
     * @param response 
     */
    @RequestMapping(value="/loadClubList.do",method= RequestMethod.GET)
    public void loadClubList(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        int type = WebUtils.paramInt(request, "type", OClubTag.TYPE_BRAND);
        int pageNo = WebUtils.paramInt(request, "pageNo", DEF_PAGE_NUM);
        int pageSize = WebUtils.paramInt(request, "pageSize", DEF_PAGE_SIZE, MIN_PAGE_SIZE, MAX_PAGE_SIZE);
        String callback = WebUtils.param(request, "callback", "");
        Pager<OClub> pager = oClubService.pagerClubByTag(type,pageNo,pageSize,OClub.STATUS_NORMAL);
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        if(pager!=null){
            for(OClub oClub : pager.getResultList()){
                array.add(oClub.getJSON(true));
            }
        }
        obj.put("data", array);
        //放入分页信息
        ActionKit.putPagerMessage(obj, pager);
        ActionKit.printSuccessMsg(response, obj, callback);
    }
    /**
     * 车友圈(车友圈即分类是主题的)
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/themeList.do",method= RequestMethod.GET)
    public String themeList(HttpServletRequest request,HttpServletResponse response){
         Pager<OClub> pager = oClubService.pagerClubByTag(OClubTag.CYQ, DEF_PAGE_NUM, DEF_PAGE_SIZE,OClub.STATUS_NORMAL);
         request.setAttribute("pager", pager);
         return "template/official/wap/club/themeList";
    }
}
